﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Torch.WL.Web
{
    public class WebCommon
    {
        public static int ConvertToInt(object Data)
        {
            try
            {
                return Convert.ToInt32(Data);
            }
            catch
            {
                return 0;
            }
        }
        public static DateTime ConvertToDateTime(object Data, DateTime DefaulValue)
        {
            try
            {
                return Convert.ToDateTime(Data);
            }
            catch
            {
                return DefaulValue;
            }
        }
        public static decimal ConvertToDecimal(object Data)
        {
            try
            {
                return Convert.ToDecimal(Data);
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 数字转大写
        /// </summary>
        /// <param name="digit"></param>
        /// <returns></returns>
        public static string ConvertToBigDigit(decimal digit, bool needUnit)
        {
            string[] num = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" };
            string[] d = { "角", "元", "拾", "佰", "仟", "万" };
            string result = "整";
            if (!needUnit)
            {
                d[0] = "";
                d[1] = "";
                result = "";
            }

            int n = (int)(digit * 10);
            int i = 0;
            bool IsZero = true;
            while (n != 0)
            {
                if (n % 10 != 0)
                {
                    result = num[n % 10] + d[i] + result;
                    if (i > 0)
                        IsZero = false;
                }
                else
                {
                    if (!IsZero)
                    {
                        IsZero = true;
                        result = '零' + result;
                    }
                    if (i == 1)
                    {
                        result = '元' + result;
                    }
                }

                i++;
                n /= 10;
                if (i > 5)
                    break;
            }
            return result;
        }

        public static void ShowMessage(System.Web.UI.Page page, string message)
        {
            string msg = WebCommon.CsStyledEncode(message);
            page.ClientScript.RegisterStartupScript(page.GetType(), "MsgScript", "<script language='javascript'>" +
                "function AlertMsg()" +
                "{" +
                "if(document.readyState=='complete')" +
                "alert('" + msg + "');" +
                "else " +
                "setTimeout('AlertMsg()',1);" +
                "}" +
                "AlertMsg();" +
                "</script>");
        }

        public static void ShowMessageForAjax(System.Web.UI.Page page, string message, Control ctr)
        {
            string msg = WebCommon.CsStyledEncode(message);
            ScriptManager.RegisterStartupScript(ctr, page.GetType(), "MsgScript", "<script language='javascript'>" +
                "function AlertMsg()" +
                "{" +
                "if(document.readyState=='complete')" +
                "alert('" + msg + "');" +
                "else " +
                "setTimeout('AlertMsg()',1);" +
                "}" +
                "AlertMsg();" +
                "</script>", false);
        }

        public static string UrlNocache(string url)
        {
            if (url.IndexOf("?") > 0)
                url += "&";
            else
                url += "?";
            url += "nocache=" + new Random().Next(10000);
            return url;
        }

        public static string CsStyledEncode(string s)
        {
            s = s.Replace("\\", "\\\\").Replace("'", "\\'").Replace("\"", "\\\"").Replace("\r\n", "\\n").Replace("\n\r", "\\n").Replace("\r", "\\n").Replace("\n", "\\n");
            s = s.Replace("/", "\\/");
            return s;
        }

        public static bool UploadFile(FileUpload thisUp, out string fileName, out string msg)
        {
            msg = "";
            //----上传
            fileName = "";
            string showName = "";
            int filesize = 0;
            if (!thisUp.HasFile)
            {
                //msg = "未选择需要上传的文件";
                return true;
            }
            try
            {
                //'检查文件扩展名字
                HttpPostedFile postedFile = thisUp.PostedFile;
                string fileExtension;
                showName = System.IO.Path.GetFileName(postedFile.FileName);
                fileExtension = System.IO.Path.GetExtension(showName);
                if (string.IsNullOrEmpty(fileExtension) || !CheckFileExt(fileExtension))
                {
                    msg = "上传的文件类型限gif、jpg、jpeg、png、bmp";
                    return false;
                }

                /*
                if (postedFile.ContentLength > (1024 * 1024*2))
                {
                    PageHelper.ShowMessagebox(this.Page, "上传的文件大小请在2MB内");
                    return;
                 }
                 */

                filesize = postedFile.ContentLength;
                fileName = System.Guid.NewGuid().ToString() + fileExtension;
                string strImgDir = AppSettingsHelper.GetString("WebsiteImagePath");
                string savePath = string.Format("{0}\\{1}", strImgDir, fileName);
                postedFile.SaveAs(savePath);

                return true;
            }

            catch (System.Exception Ex)
            {
                msg = "上传文件失败" + Ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 检查是否为合法的上传文件
        /// </summary>
        /// <param name="_fileExt"></param>
        /// <returns></returns>
        public static bool CheckFileExt(string _fileExt)
        {
            string[] allowExt = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i] == _fileExt) { return true; }
            }
            return false;

        }

        public static void CloseDialog(Page page, string dialogId)
        {
            string script = string.Format(@"$('#{0}').dialog('close');", dialogId);
            ScriptManager.RegisterClientScriptBlock(page, typeof(Page), Guid.NewGuid().ToString(), script, true);
        }

        public static void ShowDialog(Page page, string funcName)
        {
            string script = string.Format(@"{0}(2);", funcName);
            ScriptManager.RegisterClientScriptBlock(page, typeof(Page), Guid.NewGuid().ToString(), script, true);
        }

        public static void ShowDialog(Page page, string funcName, string paraValue)
        {
            string script = string.Format(@"{0}('{1}');", funcName, paraValue);
            ScriptManager.RegisterClientScriptBlock(page, typeof(Page), Guid.NewGuid().ToString(), script, true);
        }

        public static string ChineseCap(string ChineseStr)
        {
            string Capstr = "";
            byte[] ZW = new byte[2];
            long ChineseStr_int;
            string CharStr, ChinaStr = "";
            for (int i = 0; i <= ChineseStr.Length - 1; i++)
            {
                CharStr = ChineseStr.Substring(i, 1).ToString();
                ZW = System.Text.Encoding.Default.GetBytes(CharStr);
                // 得到汉字符的字节数组
                if (ZW.Length == 2)
                {
                    int i1 = (short)(ZW[0]);
                    int i2 = (short)(ZW[1]);
                    ChineseStr_int = i1 * 256 + i2;
                    #region 比较
                    if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
                    {
                        ChinaStr = "a";
                    }
                    else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
                    {
                        ChinaStr = "b";
                    }
                    else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
                    {
                        ChinaStr = "c";

                    }
                    else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
                    {
                        ChinaStr = "d";
                    }
                    else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
                    {
                        ChinaStr = "e";
                    }
                    else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
                    {
                        ChinaStr = "f";
                    }
                    else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
                    {
                        ChinaStr = "g";
                    }
                    else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
                    {

                        ChinaStr = "h";
                    }

                    else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
                    {
                        ChinaStr = "j";
                    }
                    else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
                    {
                        ChinaStr = "k";
                    }
                    else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
                    {
                        ChinaStr = "l";
                    }
                    else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
                    {
                        ChinaStr = "m";
                    }

                    else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
                    {
                        ChinaStr = "n";

                    }
                    else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
                    {
                        ChinaStr = "o";
                    }
                    else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
                    {
                        ChinaStr = "p";

                    }
                    else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
                    {
                        ChinaStr = "q";

                    }

                    else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
                    {
                        ChinaStr = "r";
                    }
                    else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
                    {
                        ChinaStr = "s";
                    }
                    else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
                    {
                        ChinaStr = "t";
                    }
                    else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
                    {
                        ChinaStr = "w";
                    }
                    else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
                    {
                        ChinaStr = "x";
                    }
                    else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
                    {
                        ChinaStr = "y";
                    }
                    else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
                    {
                        ChinaStr = "z";
                    }
                    #endregion
                }
                else
                {
                    // Capstr = ChineseStr;
                    Capstr = Capstr + CharStr;
                    // break;
                }

                Capstr = Capstr + ChinaStr;
            }

            return Capstr;

        }
    }
}